package com.etrans.web.gps.business.system.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.etrans.web.gps.base.controller.BaseController;
import com.etrans.web.gps.business.system.domain.QuestionVO;
import com.etrans.web.gps.business.system.service.QuestionService;
import com.etrans.web.gps.common.Constant;
import com.etrans.web.gps.common.page.PageResultVo;
import com.etrans.web.gps.common.page.PageVo;
import com.etrans.web.gps.util.grid.FlexiGridUtil;
import com.etrans.web.gps.util.grid.ParamMap;
import com.etrans.web.gps.util.grid.ParamValueObject;

/**
 *	题库管理
 * @author jw.chen
 *
 */
@Controller
@RequestMapping("/system/question/*")
public class QuestionController extends BaseController {
	
	@Autowired
	private QuestionService questionService;
	
	//跳转到业户查岗页面
	@RequestMapping(value = "toQuestion.html")
	public ModelAndView listQuestions(HttpServletRequest request, Model model) {
		ModelAndView mnv = new ModelAndView();
		mnv.setViewName("views/business/system/question/queryQuestion");
		return mnv;
	}
	
	//查询所有用户
	@RequestMapping(value="loadAllQuestions.html")
	public void loadAllQuestions(HttpServletResponse response,HttpServletRequest request,String paramsGrid){
		try {	
			ParamMap param = FlexiGridUtil.parseJSONParam(paramsGrid);
			ParamValueObject inputParam = this.parseJSONParam(param);
			HashMap<String, Object> params=inputParam.getMapParams();
			List<QuestionVO> rows = questionService.queryAllQuestions(params);
			Long total = questionService.allQuestionsCount(params);
			
			PageVo pageBean = new PageVo();
			pageBean.setRows(rows);
			pageBean.setPage(inputParam.getFromNumber());
			pageBean.setTotal(total.intValue());
			
			ajaxResultJson(response,pageBean);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	//保存用户信息
	@RequestMapping(value = "saveQuestion.html")
	public void saveQuestion(HttpServletRequest request,HttpServletResponse response, QuestionVO question) {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		PageResultVo pr = new PageResultVo();
		try {
			long count = questionService.checkQuestionName(question.getQuestion());
			if(count !=0 ){
				pr.setCode(Constant.CHECK_THE_SAME_NAME);
			}else{
				question.setCreate_time(format.format(new Date()));
				questionService.saveQuestion(question);
				pr.setCode(Constant.RETURN_OK_CODE);
			}
		} catch (Exception e) {
			e.printStackTrace();
			pr.setCode(Constant.RETURN_ERROR_CODE);
			pr.setMsg("保存失败!");
		}
		ajaxResultJson(response, pr);
	}
	
	//删除用户信息
	@RequestMapping(value="deleteQuestion.html")
	public void deleteQuestion(HttpServletResponse response,String questionIds){
		PageResultVo pr = new PageResultVo();
		try {
			questionIds=questionIds.replaceAll("row", "");
			if(questionIds != null && !"".equals(questionIds)){
				String[] questionId = questionIds.split(",");
				for (String s : questionId) {
					// 删除时判断是否该问题有查岗策略
					long count = questionService.findStrategyByQuestionId(Integer.parseInt(s));
					if(count > 0){
						pr.setCode(3);
					}else{
						questionService.deleteQuestion(Integer.parseInt(s));
						pr.setCode(Constant.RETURN_OK_CODE);
					}
				}
			}
					} catch (Exception e) {
			e.printStackTrace();
			pr.setCode(Constant.RETURN_ERROR_CODE);
			pr.setMsg("删除失败!");
		}
		
		ajaxResultJson(response, pr);
	}
}
